package graficar;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import graficar.GraficoTerminalInterna;

public class GraficoElipse {
	
	double x;
	double y;
	double tamX;
	double tamY;
	Ellipse2D eliAux;
	String id;
	
	
	public GraficoElipse(){
		
	}
	
	public GraficoElipse(double x, double y, double tamX, double tamY){
		this.x=x;
		this.y=y;
		this.tamX=tamX;
		this.tamY=tamY;
		this.eliAux = new Ellipse2D.Double(x,y,tamX,tamY);
	}
	
	public GraficoElipse(double x, double y, double tamX, double tamY, String id){
		this.x=x;
		this.y=y;
		this.tamX=tamX;
		this.tamY=tamY;
		this.id=id;
		this.eliAux = new Ellipse2D.Double(x,y,tamX,tamY);
	}
	
	public double getX() {
		return x;
	}
	public double getY() {
		return y;
	}
	public double getTamX() {
		return tamX;
	}
	public double getTamY() {
		return tamY;
	}
	
	public Ellipse2D getEliAux() {
		return eliAux;
	}
	
	public void setElipse(Diagrama lienzo){
		
		double ancho = lienzo.getWidth();
		double alto = lienzo.getHeight();
		
		x=(.05*ancho);
		y=(.05*alto);
		tamX=(ancho-(.1*ancho));
		tamY=(alto-(.1*alto));
		this.eliAux = new Ellipse2D.Double(x,y,tamX,tamY);
	}
	
	public ArrayList<GraficoElipse> setElipseTer(Ellipse2D eliPrincipal, GraficoConmutadorCentral cc, ArrayList<GraficoConmutadorInterno> lisCi, Diagrama lienzo){
		
		ArrayList<GraficoElipse> lisAux = new ArrayList<GraficoElipse>();
		double minX;
		double minY;
		double radX;
		double radY;
		double ancho = lienzo.getWidth();
		double alto = lienzo.getHeight();
		double varX = GraficoTerminalInterna.VARIACION_X;
		double varY = GraficoTerminalInterna.VARIACION_Y;
		
		
		for(int i=0;i<lisCi.size();i++){
			if(!(lisCi.get(i).getTi().isEmpty())){
				minX=Math.random()*(eliPrincipal.getMaxX() - eliPrincipal.getX()+1)+eliPrincipal.getX();
				minY=Math.random()*(eliPrincipal.getMaxY() - (.5*eliPrincipal.getMaxY())+1)+(.5*eliPrincipal.getMaxY());
				radX = ((ancho*varX) * lisCi.get(i).getTi().size());
				radY = ((alto*varY) * lisCi.get(i).getTi().size());
				if(eliPrincipal.contains(new Rectangle2D.Double(minX,minY,radX,radY)) &&
						noSeIntersecta(new GraficoElipse(minX, minY, radX, radY), lisCi, lisAux)){
					lisAux.add(new GraficoElipse(minX, minY, radX, radY,lisCi.get(i).id));
				}
				else{
					i-=1;
				}
			}
		}
		return lisAux;
	}
	
	public Boolean noSeIntersecta(GraficoElipse eli, ArrayList<GraficoConmutadorInterno> listCi, ArrayList<GraficoElipse> lisEli){
		for(int i=0; i<listCi.size();i++){
			if(new Rectangle2D.Double(eli.x,eli.y,eli.tamX,eli.tamY).intersects(new Rectangle2D.Double(listCi.get(i).x,listCi.get(i).y,listCi.get(i).tamX,listCi.get(i).tamY)))
				return false;
		}
		
		for(int i=0; i<lisEli.size(); i++){
			if(new Rectangle2D.Double(eli.x,eli.y,eli.tamX,eli.tamY).intersects(new Rectangle2D.Double(lisEli.get(i).getX(),lisEli.get(i).getY(),lisEli.get(i).getTamX(),lisEli.get(i).getTamY())))
				return false;
		}
		
		return true;
	}
	
	public Ellipse2D paint(Graphics g, Diagrama lienzo){
		Graphics2D g2d = (Graphics2D) g;
		double ancho = lienzo.getWidth();
		double alto = lienzo.getHeight();
		float dash[]={(float) (0.00002*(ancho*alto))};
		
		g2d.setStroke(new BasicStroke(3f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER, 5f, dash, 0.0f));
		g2d.draw(eliAux);
		
		return eliAux;
	}
	
	public void paint(Graphics g, ArrayList<GraficoElipse> lisAux){
		Graphics2D g2d = (Graphics2D) g;
		
		for(int i=0;i<lisAux.size();i++){
			g2d.draw(lisAux.get(i).eliAux);
		}
	}

}
